package com.wrh.draw.legend;

import java.awt.*;
import java.util.Arrays;
import java.util.Locale;
import java.util.regex.Pattern;

/**
 * @author wrh
 */
public class HexColorUtil {

    private static final String HEXADECIMAL_REGEX = "^#([A-Fa-f0-9]{8}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$";
    private static final Pattern HEXADECIMAL_REGEX_PATTERN = Pattern.compile(HEXADECIMAL_REGEX);


    /**
     * 校验16进制颜色字符串合法性
     *
     * @param hexadecimalColor 16进制颜色字符串
     * @return boolean
     */
    public static boolean validateHexadecimalColor(String hexadecimalColor) {
        return HEXADECIMAL_REGEX_PATTERN.matcher(hexadecimalColor).matches();
    }

    public static String convertRgbaToHex(int r, int g, int b, int a) {
        testColorValueRange(r, g, b, a);
        return "#" + toHexString(r) + toHexString(g) + toHexString(b) + toHexString(a);
    }

    public static String convertRgbToHex(int r, int g, int b) {
        return convertRgbaToHex(r, g, b, 255);
    }

    public static String convertColorToHex(Color color) {
        return convertRgbaToHex(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha());
    }

    public static Color convertHexToColor(String hex) {
        testValidateHexadecimalColor(hex);
        String r = hex.substring(1, 3);
        String g = hex.substring(3, 5);
        String b = hex.substring(5, 7);
        String a = hex.length() == 9 ? hex.substring(7, 9) : "FF";
        return new Color(get10HexInt(r), get10HexInt(g), get10HexInt(b), get10HexInt(a));
    }

    /**
     * 校验16进制字符串合法性
     *
     * @param hexadecimalColor 16进制字符串
     */
    public static void testValidateHexadecimalColor(String hexadecimalColor) {
        if (!validateHexadecimalColor(hexadecimalColor)) {
            throw new IllegalArgumentException("16进制颜色校验失败:"
                    + hexadecimalColor);
        }
    }

    /**
     * Checks the color integer components supplied for validity.
     * Throws an {@link IllegalArgumentException} if the value is out of
     * range.
     *
     * @param r the Red component
     * @param g the Green component
     * @param b the Blue component
     **/
    public static void testColorValueRange(int r, int g, int b, int a) {
        boolean rangeError = false;
        String badComponentString = "";

        if (a < 0 || a > 255) {
            rangeError = true;
            badComponentString = badComponentString + " Alpha";
        }
        if (r < 0 || r > 255) {
            rangeError = true;
            badComponentString = badComponentString + " Red";
        }
        if (g < 0 || g > 255) {
            rangeError = true;
            badComponentString = badComponentString + " Green";
        }
        if (b < 0 || b > 255) {
            rangeError = true;
            badComponentString = badComponentString + " Blue";
        }
        if (rangeError) {
            throw new IllegalArgumentException("rgba值不在0~255范围内:"
                    + badComponentString);
        }
    }


    /**
     * 获取16进制字符串
     */
    public static String toHexString(int v) {
        return Integer.toHexString(v).toUpperCase(Locale.ROOT);
    }

    /**
     * 获取10进制整数
     */
    public static int get10HexInt(String v) {
        return Integer.parseInt(v, 16);
    }


    public static HexColor[] getHexColorArray(String... hexs) {
        return Arrays.stream(hexs).map(HexColor::new).toArray(HexColor[]::new);
    }
}
